package zhoug.chart.utils;

import android.graphics.PointF;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.view.ViewParent;

import com.github.mikephil.charting.charts.Chart;
import zhoug.chart.Chart2Sdk;
import zhoug.logging.Logger;

import androidx.viewpager.widget.ViewPager;

/**
 * 图表在ViewPager和fragment中滑动冲突解决方案
 * 注意:当图表在SwipeRefreshLayout中时,requestDisallowInterceptTouchEvent事件被重写了,所以单独获取ViewPager来调用打断时间
 *
 * @Author 35574
 * @Date 2020/10/12
 * @Description
 */
 public class MPChartScrollHelper {
    private static final String TAG = ">>>ChartScrollHelper";
    private static  boolean DEBUG= Chart2Sdk.isDebug();

    private Chart mChart;
    private PointF mDownPoint = new PointF();
    private float distanceX;
    private ViewPager viewPager;
    private int mTouchSlop;

    public MPChartScrollHelper(Chart chart) {
        this.mChart = chart;
        mTouchSlop = ViewConfiguration.get(chart.getContext()).getScaledTouchSlop() / 2;
    }


    /**
     * 找到viewPager
     *
     * @param parent
     */
    public void findViewPager(ViewParent parent) {
        if (parent != null) {
            if (parent instanceof ViewPager) {
                viewPager = (ViewPager) parent;
            } else {
                findViewPager(parent.getParent());
            }
        }
    }

    private int firstId = -1;

    public  void onTouchEvent(MotionEvent event) {
        try {
            switch (event.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                    if (DEBUG) {
                        Logger.d(TAG, "onTouchEvent:ACTION_DOWN");
                    }
                    mDownPoint.x = event.getX();
                    mDownPoint.y = event.getY();
                    firstId = event.getPointerId(event.getActionIndex());
                   /* if(mChart.getScaleX() > 1){
                        requestDisallowInterceptTouchEvent(true);
                    }*/
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (DEBUG) {
                        Logger.d(TAG, "onTouchEvent:ACTION_MOVE");
                    }
                    int pointerIndex = event.findPointerIndex(firstId);
                    if (pointerIndex >= 0) {
                        distanceX = Math.abs(event.getX(pointerIndex) - mDownPoint.x);
                        if (DEBUG) {
                            Logger.d(TAG, "onTouchEvent:distanceX=" + distanceX);
                        }
                        //x轴放大&水平移动距离大于垂直移动距离&水平移动距离大于3
                        if (mChart.getScaleX() > 1 &&
                                (distanceX >= Math.abs(event.getY(pointerIndex) - mDownPoint.y))
                                && distanceX >= 1) {
                            requestDisallowInterceptTouchEvent(true);
                        }
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    firstId = -1;
                    if (DEBUG) {
                        Logger.d(TAG, "onTouchEvent:ACTION_UP");
                    }
                    requestDisallowInterceptTouchEvent(false);
                    break;
                case MotionEvent.ACTION_CANCEL:
                    if (DEBUG) {
                        Logger.d(TAG, "onTouchEvent:ACTION_CANCEL");
                    }
                    firstId = -1;
                    requestDisallowInterceptTouchEvent(false);
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 禁止父类拦截触摸事件
     *
     * @param d
     */
    private void requestDisallowInterceptTouchEvent(boolean d) {
        ViewParent parent = mChart.getParent();
        if (DEBUG) {
            Logger.d(TAG, "requestDisallowInterceptTouchEvent:parent=" + parent);
        }
        if (parent != null) {
            if (DEBUG) {
                Logger.d(TAG, "requestDisallowInterceptTouchEvent:禁止父类拦截触摸事件:" + d);
            }
            parent.requestDisallowInterceptTouchEvent(d);
        }
        if (viewPager != null) {
            viewPager.requestDisallowInterceptTouchEvent(d);
        }
    }

}